package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.UUID;

import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.icis.blo.IChkLogService;
import com.shine.icis.blo.IStockCheckService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dao.param.ETLRunLogQueryParameters;
import com.shine.icis.hbean.ChkRunLog;

public class ChkLogAction extends BaseAction {

	/**
	 * 日志查询
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private IChkLogService chkLogService;
	private IStockCheckService stockCheckService;
	
	//input param
	private int pageNo;
	private int pageSize;
	private String operDate;//日期
	private Integer etlProcID;//过程
	private Integer prepLevelID;//警告级别
	private String expType;//导出方式
	
	//output param
	
	private String defaultDate;//当前交易日
	
	private ListPage<ChkRunLog> chkLogPage;
	
	private String downloadUrl;//下载地址
	
	public void setChkLogService(IChkLogService chkLogService) {
		this.chkLogService = chkLogService;
	}
	public void setStockCheckService(IStockCheckService stockCheckService) {
		this.stockCheckService = stockCheckService;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setOperDate(String operDate) {
		this.operDate = operDate;
	}

	public void setEtlProcID(Integer etlProcID) {
		this.etlProcID = etlProcID;
	}

	public void setPrepLevelID(Integer prepLevelID) {
		this.prepLevelID = prepLevelID;
	}
	
	public void setExpType(String expType) {
		this.expType = expType;
	}
	
	public ListPage<ChkRunLog> getChkLogPage() {
		return chkLogPage;
	}

	public String getDefaultDate() {
		return defaultDate;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}
	/**
	 * 转到采集运行日志页面
	 * @return
	 */
	public String initChkRunLog() {
		defaultDate = stockCheckService.getCurrentDate();
		return success();
	}
	
	/**
	 * 采集运行日志
	 * @return
	 */
	public String queryChkRunLog() {
		ETLRunLogQueryParameters pqp = new ETLRunLogQueryParameters();
		
		//设置分页信息
		pqp.setPageNo(pageNo);
		pqp.setPageSize(pageSize);
		//增加对排序列的处理
		pqp.addOrder("operDate", false);
		pqp.addOrder("operTime", false);
		pqp.addOrder("logID", false);
		
		//设置查询参数
		
		if(operDate != null && !operDate.trim().equals("")){
			operDate = operDate.trim().replaceAll("-", "");
			pqp.setOperDate(Integer.valueOf(operDate));
		}
		if(etlProcID != null){
			pqp.setEtlProcID(etlProcID);
		}
		if(prepLevelID != null){
			pqp.setPrepLevelID(prepLevelID);
		}
		try {
			chkLogPage = chkLogService.queryChkRunLogPage(pqp);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 导出Excel
	 */
	public String exportRunLog() {
		ETLRunLogQueryParameters pqp = new ETLRunLogQueryParameters();
		
		//增加对排序列的处理
		pqp.addOrder("operDate", false);
		pqp.addOrder("operTime", false);
		pqp.addOrder("logID", false);
		
		//设置查询参数
		
		if(operDate != null && !operDate.trim().equals("")){
			operDate = operDate.trim().replaceAll("-", "");
			pqp.setOperDate(Integer.valueOf(operDate));
		}
		if(etlProcID != null){
			pqp.setEtlProcID(etlProcID);
		}
		if(prepLevelID != null){
			pqp.setPrepLevelID(prepLevelID);
		}
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = this.chkLogService.csExpChkRunLog(pqp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}

}
